package com.xiaozhen.driver.controller;

import com.xiaozhen.common.core.domain.AjaxResult;
import com.xiaozhen.common.core.domain.driver.ParseDriver;
import com.xiaozhen.driver.service.ParseDriverService;
import com.xiaozhen.security.controller.BaseController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

/**
 * HISTORY
 *
 * @author zhenweisheng
 * @date 2021/11/3 10:31
 * @description:
 */
@RestController
@Api(tags = "驱动管理接口")
@RequestMapping("/driver")
public class ParseDriverController extends BaseController {

    @Autowired
    private ParseDriverService parseDriverService;

    @ApiOperation("获取驱动列表")
    @GetMapping("/list")
    public AjaxResult list(ParseDriver param) {
        List<ParseDriver> attributes = parseDriverService.selectDriverList(param);
        return AjaxResult.success(attributes);
    }

    @ApiOperation("添加驱动")
    @PostMapping("/add")
    public AjaxResult add(@Validated @RequestBody ParseDriver driver) {
        if (!parseDriverService.checkDriverNameUnique(driver)) {
            return AjaxResult.error("新增驱动'" + driver.getDriverName() + "'失败，驱动名称已存在");
        }
        driver.setCreateBy(getCurrentUserId());
        driver.setCreateTime(LocalDateTime.now());
        return toAjax(parseDriverService.insertDriver(driver));
    }

    @ApiOperation("修改驱动")
    @PutMapping("/update")
    public AjaxResult edit(@Validated @RequestBody ParseDriver driver) {
        if (!parseDriverService.checkDriverNameUnique(driver)) {
            return AjaxResult.error("新增驱动'" + driver.getDriverName() + "'失败，驱动名称已存在");
        }
        driver.setUpdateBy(getCurrentUserId());
        driver.setUpdateTime(LocalDateTime.now());
        return toAjax(parseDriverService.updateDriver(driver));
    }

    @ApiOperation("删除驱动")
    @DeleteMapping("/{driverId}")
    public AjaxResult remove(@PathVariable("driverId") Long driverId) {
        return toAjax(parseDriverService.deleteDriverById(driverId));
    }
}
